import json


def read_data(filename):
    with open(filename, 'r', encoding='utf-8') as f:
        return json.load(f)


def get_cookies():
    cookies = read_data('../data/y_admin_cookies.json')
    # print(cookies)
    return cookies


navigation_rank_arr = ['一级栏目...', 'ShopXO', '商品分类']


# 处理获取到的数据，将之转换为测试代码中可用的数据
def processing_add_data(d):
    rank = 1
    is_show = False
    is_new = False

    for i in range(len(navigation_rank_arr)):
        if d['rank'] == navigation_rank_arr[i]:
            rank = i + 1

    if d['show'] == '显示':
        is_show = True

    if d['view_new'] == '是':
        is_new = True
    return rank, is_show, is_new


# 获取网站添加自定义导航的数据
def get_navigation_add_custom():
    data = read_data('../data/y_admin_navigation_add.json')['custom']
    arr = []
    for d in data:
        p_data = processing_add_data(d)
        arr.append([(p_data[0], d['name'], d['url'], d['sequence'], p_data[1], p_data[2]), d['expect']])
    # print(arr)
    return arr


navigation_article_arr = [
    None, None, '如何注册成为会员', '积分细则', '如何搜索',
    None, '如何管理店铺', '查看售出商品', '如何发货', '商城商品推荐', '如何申请开店',
    None, '分期付款', '邮局汇款', '公司转账', '如何注册支付宝', '在线支付',
    None, '积分兑换说明', '忘记密码', '联系卖家', '退换货政策', '退换货流程', '返修/退换货', '退款申请',
    None, '会员修改密码', '会员修改个人资料', '商品发布', '修改收货地址',
    None, '合作及洽谈', '招聘英才', '联系我们', '关于ShopXO'
]


# 获取网站添加文章导航的数据
def get_navigation_add_article():
    data = read_data('../data/y_admin_navigation_add.json')['article']
    arr = []
    for d in data:
        p_data = processing_add_article_data(d, navigation_article_arr)
        arr.append([(p_data[0], p_data[1], d['name'], d['sequence'], p_data[2], p_data[3]), d['expect']])
    # print(arr)
    return arr


# 处理文章导航添加数据
def processing_add_article_data(d, data):
    article = 1
    for i in range(len(data)):
        if d['article'] == data[i]:
            article = i + 1
    result = processing_add_data(d)
    # print(result[0], article, result[1], result[2])
    return result[0], article, result[1], result[2]


# 获取网站管理——快递管理——（添加）数据
def get_express_add_data():
    data = read_data('../data/y_admin_web_express.json')['express_add']
    arr = []
    for d in data:
        arr.append(((d['name'], d['sequence'], d['is_use']), d['expect']))
    return arr


# 获取网站管理——快递管理——（删除）数据
def get_express_delete_data():
    data = read_data('../data/y_admin_web_express.json')['express_tr_delete']
    arr = []
    for d in data:
        arr.append((d['num'], d['expect']))
    return arr


if __name__ == '__main__':
    # get_navigation_add_article()
    get_express_add_data()
